<?php

/**
 * GrouponController is the default controller for this application
 */
class GrouponController extends BrowseControllerBase {
	/**
	 * GrouponDataService object
	 *
	 * @var GrouponDataService object
	 */
	public $grouponDataService = null;
	public $dealsDataService = null;
	/**
	 * GrouponCommentDataService object
	 *
	 * @var GrouponCommentDataService object
	 */
	public $grouponCommentDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'groupon' );
		
		// Initialise GrouponDataService
		$this->grouponDataService = new GrouponDataService ();
		//
		$this->dealsDataService = new DealsDataService ();
	
		// Initialise GrouponCommentDataService
	//		$this->grouponCommentDataService = new GrouponCommentDataService();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		//Type maybe next_id or prev_id
		$type = $this->getQuery ( 'type' );
		if (! empty ( $type )) {
			$keywords = explode ( '_', $type );
			
			$id = "";
			$order = "";
			if (count ( $keywords ) == 2) {
				$id = $keywords [1];
				$order = $keywords [0];
			}
			
			if ($order == "next") {
				$itemsList = $this->grouponDataService->getNextEntry ( $id, 5 );
			
			} else if ($order == "prev") {
				$itemsList = $this->grouponDataService->getPreviousEntry ( $id, 5 );
			}
		} else {
			// Get 5 latest Groupon entry
			$arrElement = array ();
			$arrElement ['status'] = GrouponEntity::ACTIVE;
			$itemsList = $this->grouponDataService->getItems ( $arrElement, " id DESC", 0, 5 );
		
		}
		
		$hasNextEntries = false;
		$hasPreviousEntries = false;
		
		if (is_array ( $itemsList ) && count ( $itemsList ) > 0) {
			$start_id = end ( $itemsList )->id;
			$end_id = $itemsList [0]->id;
			$this->smarty->assign ( 'end_id', $end_id );
			$this->smarty->assign ( 'start_id', $start_id );
			
			$nextItemsList = $this->grouponDataService->getNextEntry ( $end_id, 5 );
			
			$prevItemsList = $this->grouponDataService->getPreviousEntry ( $start_id, 5 );
			if (is_array ( $itemsList ) && count ( $nextItemsList ) > 0) {
				$hasNextEntries = true;
			}
			
			if (is_array ( $itemsList ) && count ( $prevItemsList ) > 0) {
				$hasPreviousEntries = true;
			}
		
		}
		$this->smarty->assign ( 'grouponItemList', $itemsList );
		/*		
		if(count($itemsList) == 1)
		{
			$arrTemp = array();
			$arrTemp[] = $itemsList;
			$this->smarty->assign('grouponItemList', $arrTemp);
		}
		*/
		$this->smarty->assign ( 'hasPreviousEntries', $hasPreviousEntries );
		$this->smarty->assign ( 'hasNextEntries', $hasNextEntries );
		
		$this->smarty->assign ( 'numberGrouponItemList', count ( $itemsList ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Groupon/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function detailsAction() {
		$id = $this->getQuery ( 'id' );
		if ($id == '') {
			$id = $this->getPost ( 'id', '' );
		}
		$id = intval ( $id );
		//get Deal
		$dealsEntity = $this->dealsDataService->getById ( $id );
		// Get data
		$grouponEntity = $this->grouponDataService->getById ( $dealsEntity->groupon_id );
		if (empty ( $grouponEntity )) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL;
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$this->smarty->assign ( 'grouponEntity', $grouponEntity );
		$this->smarty->assign ( 'dealsEntity', $dealsEntity );
		$actionForm = $this->getPost ( 'actionForm', '' );
		//echo $actionForm;
		//		if (!empty($actionForm) && $actionForm == 'submit'){
		//			$author = $this->getPost('author','');
		//			$email  = $this->getPost('email','');
		//			$website  = $this->getPost('url','');
		//			$comment = $this->getPost('comment','');
		//
		//			$grouponCommentEntity = $this->grouponCommentDataService->beforeCreate();
		//			$grouponCommentEntity->name = $author;
		//			$grouponCommentEntity->grouponId = $id;
		//			$grouponCommentEntity->email = $email;
		//			$grouponCommentEntity->website = $website;
		//			$grouponCommentEntity->content = $comment;
		//			$grouponCommentEntity->commentId = 0;
		//			$grouponCommentEntity->status = GrouponCommentEntity::ACTIVE;
		//			$this->grouponCommentDataService->create($grouponCommentEntity);
		//
		//			//Update CommentCount for Groupon Entry
		//			$grouponEntity->commentCount = $grouponEntity->commentCount + 1;
		//			$this->grouponDataService->update($grouponEntity);
		//
		//			$message =  'Your request has just sent successfully';
		//		}
		//
		//
		//
		//		// Get All Comments of that Groupon Entry
		//		$listComments = $this->grouponCommentDataService->getAllQuestions($id);
		//
		//		// Get Next and Previous Groupon Entry
		//		$nextEntry = $this->grouponDataService->getNextEntry($id);
		//		$prevEntry = $this->grouponDataService->getPreviousEntry($id);
		//
		//		$this->smarty->assign('listComments',$listComments);
		//		$this->smarty->assign('id',$id);
		//		$this->smarty->assign('grouponCommentDataService',$this->grouponCommentDataService);
		//		$this->smarty->assign('nextEntryId',!empty($nextEntry) ? $nextEntry->id : "");
		//		$this->smarty->assign('previousEntryId',!empty($prevEntry) ? $prevEntry->id : "");
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Groupon/details.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
